define( [
  'require',
  'jquery',
  'api',
  './tabMaterialCollect/designLogic',
  './tabMaterialCollect/templateLogic',
  './tabMaterialCollect/imageLogic',
  './tabMaterialCollect/styleMaterialLogic',
  './tabMaterialCollect/typesetLogic',
  './tabMaterialCollect/festivalLogic',
  '../lib/utils',
  '../modules/createEditor',
  'bigwe_utils',
  'header'
], function( require, $, BIGWE_API, design, template, image, style, typeset, festival ) {
  var ut = require( '../lib/utils' );
  var bu = require( 'bigwe_utils' );
  var header = require( 'header' );
  var editorObj = require( '../modules/createEditor' );
  var newFestival = $( '#new_festival' ).val();
  var self = $( '.edit-column1' );
  // 上线样式需要初始化的数据
  if ( newFestival === '1' ) {
    festival.init();
  } else {
    // 下线样式后需要初始化的数据
    style.init();
  }

  //=========
  function picturePreviewwWidth() {
    var height = $( window ).height() - $( '.fixed-save-sycn' ).outerHeight() - header.$selector.outerHeight();
    $( '.picture_preview' ).height( height );
  }

  editorObj.ready( function() {
    $( window ).resize( picturePreviewwWidth );
    picturePreviewwWidth();
  } );

  function picturePreviewTop() {
    $( '.picture_preview' ).css( 'top', $( document ).scrollTop() )
  }

  function widthGet() {
    var toolbarbox = $( '#edui14_toolbarbox.edui-editor-toolbarbox' );
    var toolbarboxDisplay = toolbarbox.css( 'position' );
    var outerWidth = $( '.edit-column .edit-column2' ).outerWidth() - 1
    if ( $( this ).hasClass( 'selected' ) ) {
      if ( toolbarboxDisplay === 'fixed' ) {
        toolbarbox.css( {
          width: outerWidth
        } );
      }
    } else {
      if ( toolbarboxDisplay === 'fixed' ) {
        toolbarbox.css( {
          width: outerWidth
        } );
      }
    }
  }
  
  /**
   * 主题 样式等切换
   */
  $( document ).on( 'click', '.btn-list>button', function() {

    if ( $( this ).hasClass( 'selected' ) ) {
      $( this ).removeClass( 'selected' );
      $( '.edit-column>.edit-column2' ).addClass( 'open' );
      $( '.bg-div', $( this ).parent() ).hide();
      $( '.fixed-save-sycn .save-sycn-send' ).removeClass( 'active' );
    } else {
      $( '.edit-column>.edit-column2' ).removeClass( 'open' );

      $( this ).addClass( "selected" ).siblings().removeClass( "selected" );
      $( '.bg-div', $( this ).parent() ).show().transit( {
        y: $( this ).position().top,
        height: $( this ).outerHeight()
      } );

      $( '.content-list>div' ).eq( $( this ).index() ).show().siblings().hide();

      if ( $( this ).hasClass( 'js_picture' ) ) {
        image.pictureInit();
      } else if ( $( this ).hasClass( 'js_video' ) ) {
        video.videoInit();
      } else if ( $( this ).hasClass( 'js_template' ) ) {
        template.templateInit();
      } else if ( $( this ).hasClass( 'js_festival_material' ) ) {
        var targetIndex = $( this ).data( 'festival-target' ) || '';
        if ( !this.festivalMaterialLoad ) {
          this.festivalMaterialLoad = true;
          $( '.content-list .event' ).eq( targetIndex || 0 ).load( BIGWE_API.EDIT_TEXT.GET_FESTIVAL_MATERIAL_HTML + '?v=' + targetIndex );
        }
      } else if ( $( this ).hasClass( 'js_common_material' ) ) {
        style.init();
      } else if ( $( this ).hasClass( 'js_design' ) ) {
        design.designInit();
      } else if ( $( this ).hasClass( 'js_typeset' ) ) {
        typeset.init();
      } else if ( $( this ).hasClass( 'js_festival_tab' ) ) {
        festival.init();
      }
      $( '.fixed-save-sycn .save-sycn-send' ).addClass( 'active' );
    }
    widthGet.call( this );
  } ).on( 'click', '.js_colorRange_tool', function() {
    $( '#js_color_anchor' ).trigger( 'click' );
  } ).on( {
    mouseleave: function() {
      $( '.picture_preview' ).addClass( 'hide' );
    },
    mouseenter: function() {
      $( '.picture_preview' ).removeClass( 'hide' ).find( 'img' ).prop( 'src', ut.timthumbFormat( $( '.img_item', this ).attr( 'editor_url' ), 600 ) );
    }
  }, '.img_item_wrap' ).on( 'click', '.js_insertcontent', function() {
    ut.comInsertImage.call( editorObj, {
      src: ut.timthumbFormat( $( '.img_item', $( this ).parents( '.img_item_wrap' ) ).attr( 'editor_url' ), 600 )
    } );

  } ).on( 'click', '.js_insertbackgroundimg', function() {
    // 插入背景
    window.insertbackgroundimg = ut.timthumbFormat( $( '.img_item', $( this ).parents( '.img_item_wrap' ) ).attr( 'editor_url' ), 600 )
    editorObj.ui._dialogs[ 'backgroundDialog' ].open();
  } ).on( 'click', '.js_insertcover', function() {
    $( '.cover-input' ).val( bu.wechartImgCheck( $( '.img_item', $( this ).parents( '.img_item_wrap' ) ).attr( 'editor_url' ), true ) ).trigger( 'change' );
  } ).on( 'scroll', picturePreviewTop );


} );